//
//  PictureBrowerViewController.swift
//  SinaMicroblog
//
//  Created by 唐绍禹 on 15/8/10.
//  Copyright © 2015年 唐绍禹. All rights reserved.
//

import UIKit

private let PictureBrowerCell = "PictureBrowerCell"

class PictureBrowerViewController: UIViewController,UICollectionViewDataSource {
    
    var pictureURLs: [NSURL]
    var selectedIndex: Int

    init(urls:[NSURL], index: Int) {
        
        pictureURLs = urls
        selectedIndex = index
        
       super.init(nibName:nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    //视图即将加载完成的时候跳转到选中的cell
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        let index = NSIndexPath(forItem: selectedIndex, inSection: 0)
        
        collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
    }

    func close() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func save() {
        print("保存图片")
    }

    override func loadView() {
        var screenBounds = UIScreen.mainScreen().bounds
        screenBounds.size.width += 20
        
        view = UIView(frame: screenBounds)
        
    }
    
    private func setupUI() {
        view.addSubview(collectionView)
        view.addSubview(closeBtn)
        view.addSubview(saveBtn)
        
        collectionView.frame = view.bounds
        
        let rect = CGRectMake(0, UIScreen.mainScreen().bounds.height - 47, 50, 32)
        saveBtn.frame = CGRectOffset(rect, 15, 0)
        closeBtn.frame = CGRectOffset(rect, 30 + rect.width, 0)
        
        closeBtn.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        saveBtn.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        
        prepareCollectionView()
    }
    
    private func prepareCollectionView() {
        collectionView.registerClass(PictureCollectionViewCell.self, forCellWithReuseIdentifier: PictureBrowerCell)
        collectionView.dataSource = self
        
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView.pagingEnabled = true
        
    }
    
    //MARK:-设置数据源方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureURLs.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureBrowerCell, forIndexPath: indexPath) as! PictureCollectionViewCell
        
//        cell.backgroundColor = UIColor.randomColor()
        cell.imageURL = pictureURLs[indexPath.item]
        
        return cell
    }
    
    //懒加载控件
    private lazy var collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    private lazy var closeBtn: UIButton = UIButton(title: "关闭", fontsize: 14, color: UIColor.whiteColor())
    private lazy var saveBtn: UIButton = UIButton(title: "保存", fontsize: 14, color: UIColor.whiteColor())
}


